<template>
	<view class="content">
		<view v-if='schoolId' class="input-con">
			<view class="input-name">
				<view>学校名称：</view>
				<view style="margin-left: auto;color: rgb(48, 49, 51);">
					{{school}}
				</view>
			</view>
			<u-line color="#E7E7E7"></u-line>
			<!-- <view class="input-name">
				<view>学生姓名：</view>
				<view style="margin-left: auto;">
					<u--input placeholder="请输入学生姓名" v-model="name" border="none" inputAlign="right"></u--input>
				</view>
			</view>
			<u-line color="#E7E7E7"></u-line> -->
			<view class="input-name">
				<view>学生编码：</view>
				<view style="margin-left: auto;">
					<u--input placeholder="请输入学生编号" type="numer" v-model="card" border="none"
						inputAlign="right"></u--input>
				</view>
			</view>
			<u-line color="#E7E7E7"></u-line>
		</view>
		<view v-if='schoolId' class="index-but" @click="goDetai">
			查询费用明细
		</view>
		<view v-if='!schoolId'>
			请扫描学校二维码进入
		</view>
	</view>
</template>

<script>
	import {
		searchApi
	} from "@/apis/user"
	export default {
		data() {
			return {
				name: '',
				school: '',
				card: '',
				schoolId: '',
			}
		},
		onLoad(option) {
			console.log(option.school);
			console.log();
			this.school = option.school
			this.schoolId = option.schoolId
		},
		onShow() {
			// if (uni.getStorageSync('token')) {
			// 	this.checkWeChatCode()
			// }
		},
		methods: {
			async goDetai() {
				if (!this.card) {
					uni.showToast({
						title: '请输入学生编号',
						icon: 'none'
					})
					return
				}
				try {
					uni.showLoading({
						title: '查询中...'
					})
					const res = await searchApi({
						code: this.card,
						deptId: this.schoolId
					})
					uni.hideLoading()
					if (res.data.length === 0) {
						uni.showToast({
							title: '暂未查询到数据',
							icon: 'none'
						})
					} else {
						const item = res.data[0]
						uni.navigateTo({
							url: '/pages/pay/pay?item=' + encodeURIComponent(JSON.stringify(item)) +
								'&school=' + this.school
						})
					}


				} catch (e) {
					uni.hideLoading()
				}


			},
			checkWeChatCode() {
				let code = this.getUrlCode('code')
				if (code) {
					console.log(code);
					this.getOpenidAndUserinfo(code)
				} else {
					this.getCode()
				}
			},
			getUrlCode(name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||
					[, ''
					])[1]
					.replace(/\+/g, '%20')) || null
			},
			async getOpenidAndUserinfo(code) {
				const res = await loginApi({
					code
				})
				console.log(res);
				uni.setStorageSync('token', res.data.token)
				uni.setStorageSync('userInfo', res.data.userInfo)
				this.getUser()
				// window.location.href = this.local
			},
			getCode() {
				console.log(window.location.href);
				this.local = window.location.href
				let local = encodeURIComponent(window.location.href);
				// wx2a9760e7a2f935d3
				// wxca0c462b619d3b6b
				let appid = 'wxca0c462b619d3b6b'
				window.location.href =
					"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
					appid +
					"&redirect_uri=" +
					local +
					"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

			},
		}
	}
</script>

<style>
	.content {
		padding-top: 23rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.input-con {
		width: 688rpx;
		color: #666666;
		font-size: 27rpx;
	}

	.input-name {
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.index-but {
		margin-top: 90rpx;
		width: 688rpx;
		height: 85rpx;
		background: #4078FF;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 31rpx;
		color: #FFFFFF;
	}
</style>